package com.ady.collect;

import org.apache.commons.codec.binary.Base64;

/**
 * <b>description</b>：地址转换工具 <br>
 * <b>time</b>：2018-06-22 13:04 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class LinkUtil {
    public static void main(String[] args) {
        String url = "ftp://ygdy8:ygdy8@yg72.dydytt.net:8231/%E9%98%B3%E5%85%89%E7%94%B5%E5%BD%B1www.ygdy8.com.%E5%A4%B4%E5%8F%B7%E7%8E%A9%E5%AE%B6.HD.720p.%E4%B8%AD%E8%8B%B1%E5%8F%8C%E5%AD%97%E5%B9%95.mkv";
        System.out.println(getThunder(url));
        System.out.println(parseThunder(getThunder(url)));
        System.out.println(getQqdl(url));
        System.out.println(parseQqdl(getQqdl(url)));
        System.out.println(getFlashget(url));
        System.out.println(parseFlashget(getFlashget(url)));
    }

    /**
     * 将url转换为迅雷地址
     * 迅雷的编码规则为：原地址前面加"AA"，后面加"ZZ"，然后进行Base64编码，最后加上迅雷下载协议"Thunder://"组成完整的下载链接
     *
     * @param url
     * @return
     */
    public static String getThunder(String url) {
        String resource = "AA" + url + "ZZ";
        return "Thunder://" + Base64.encodeBase64String(resource.getBytes());
    }

    /**
     * 将迅雷地址换货为原始地址
     *
     * @param url
     * @return
     */
    public static String parseThunder(String url) {
        url = url.substring("Thunder://".length());
        String s = new String(Base64.decodeBase64(url));
        return s.substring("AA".length(), s.length() - "ZZ".length());
    }

    /**
     * 将url转换为快车地址
     * 快车的编码规则：原地址前后都加上"[FLASHGET]"，注意后面还要加上"&"，符号怎么得出不清楚，在最后面加的是其他个人信息，至今未有人报告转换错误
     *
     * @param url
     * @return
     */
    public static String getFlashget(String url) {
        String resource = "[FLASHGET]" + url + "[FLASHGET]";
        return "Flashget://" + Base64.encodeBase64String(resource.getBytes()) + "&something";
    }

    /**
     * 将快车址换货为原始地址
     *
     * @param url
     * @return
     */
    public static String parseFlashget(String url) {
        url = url.substring("Flashget://".length(),url.length()-"&something".length());
        String s = new String(Base64.decodeBase64(url));
        return s.substring("[FLASHGET]".length(), s.length() - "[FLASHGET]".length());
    }

    /**
     * 将url转换为QQ旋风地址
     * 旋风相对就简单多了：将原地址直接base64编码加上下载协议就好了
     *
     * @param url
     * @return
     */
    public static String getQqdl(String url) {
        String resource = url;
        return "qqdl://" + Base64.encodeBase64String(resource.getBytes());
    }

    /**
     * 将QQ旋风址换货为原始地址
     *
     * @param url
     * @return
     */
    public static String parseQqdl(String url) {
        url = url.substring("qqdl://".length());
        return new String(Base64.decodeBase64(url));
    }
}
